<?php

namespace App\Models;

class AfterSales extends Base
{
    protected $table = 'after_sales';

    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_TEXT = [
        self::TYPE_1 => '<span class="el-tag el-tag--danger">退款</span>',
        self::TYPE_2 => '<span class="el-tag">退货</span>',
    ];

    const STATE_1 = 1;
    const STATE_2 = 2;
    const STATE_3 = 3;
    const STATE_TEXT = [
        self::STATE_1 => '<span class="el-tag el-tag--danger">审核中</span>',
        self::STATE_2 => '<span class="el-tag">同意</span>',
        self::STATE_3 => '<span class="el-tag el-tag--danger">不同意</span>',
    ];

    protected $fillable = [
        'id','order_id','order_sn','refund_sn','store_id','store_name','user_id','user_name','goods_id','amount','number','type','state',
        'seller_time','user_message', 'user_pic','seller_message','invoice_no','ship_time','receive_time','created_at','updated_at',
    ];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('order_sn','like','%' . $params['kwd'] . '%')->orwhere('refund_sn','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['state']) && $params['state']) {
                    $query ->where('state',$params['state']);
                }
                if(isset($params['type']) && $params['type']) {
                    $query ->where('type',$params['type']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'type'=> self::TYPE_TEXT,
            'state' => self::STATE_TEXT,
        ]);
        return $lists;
    }

}
